import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:yw_bapp/dao/common_dao.dart';
import 'package:yw_bapp/model/check_merchant_model.dart';
import 'package:yw_bapp/utils/show_tip_pop.dart';
import 'package:yw_bapp/widgets/input_widget.dart';
import 'package:yw_bapp/widgets/m_btn.dart';
import 'package:yw_bapp/widgets/safe_area_bar_widget.dart';
import 'package:yw_bapp/widgets/show_toast.dart';

// 商户信息
class NaturalInfoPage extends StatefulWidget {
  @override
  _NaturalInfoPageState createState() => _NaturalInfoPageState();
}

class _NaturalInfoPageState extends State<NaturalInfoPage> {
  //信用代码控制器
  TextEditingController codeController = TextEditingController();
  //商户名称控制器
  TextEditingController nameController = TextEditingController();
  // 下一步按钮的颜色
  bool isActive = false;
  // 按钮点击色
  Color btnActiveColor = ColorUtil.color('#FFC49226');
  // 按钮默认色
  Color btnDefaultColor = ColorUtil.color('#FFCCCCCC');
  // 文字点击色
  Color textActiveColor = Colors.white;
  // 文字默认色
  Color textDefaultColor = ColorUtil.color("#FFAFAFAF");
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XAppBar(
        statusBarColor: ColorUtil.color('#FFC49226'),
        bgColor: ColorUtil.color('#FFC49226'),
        title: "资质信息",
        iconColor: Colors.white,
      ),
      body: Container(
        padding: EdgeInsets.only(top: 10),
        color: ColorUtil.color("#FFe0e0e0"),
        child: Column(
          children: <Widget>[
            CreateMyInput(
              leadingTitle: '法定代表人姓名',
              iconString: '',
              placeholder: "请输入法定代表人姓名",
              isPassword: false,
              inputController: codeController,
            ),
            CreateMyInput(
                leadingTitle: '法定代表人电话',
                iconString: '',
                placeholder: "请输入法定代表人电话",
                isPassword: false,
                inputController: nameController,
                typeNum: 2),
            // 图片上传组件
            Container(
              margin: EdgeInsets.only(top: 100),
              child: MBtn(
                borderColor: isActive ? textActiveColor : textDefaultColor,
                title: '下一步',
                hasBg: true,
                bgColor: isActive ? btnActiveColor : textDefaultColor,
                borderRadius: 5,
                btnWidth: 325,
                btnHeight: 44,
                function: () {
                  _toNextPage();
                },
              ),
            )
          ],
        ),
      ),
    );
  }

// 点击检查商户是否存在跳转下一步
  void _toNextPage() {
    if (codeController.text != '' && nameController.text != '') {
      setState(() {
        isActive = true;
      });
      CommonDao.CheckMerchant(codeController.text)
          .then((CheckMerchantModel model) => {
                if (model.status == 200)
                  {
                    if (model.data == null)
                      {
                        //未检查到该商户，跳转页面
                      }
                    else
                      {
                        //检查到商户，提示弹窗
                        showAlertDialog(context,
                            '法定代表人：${model.data.legalPersonName}商户名称：${model.data.name}',
                            confirmText: '确认绑定')
                      }
                  }
              });
    } else {
      setState(() {
        isActive = false;
        ShowToast.toast(context, msg: '请完善数据信息', position: ToastPostion.center);
      });
    }
  }
}
